package com.zhw.controller;


import com.zhw.config.ProjectUrlConfig;
import com.zhw.constant.CookieConstant;
import com.zhw.constant.RedisConstant;
import com.zhw.dataobject.SellerInfo;
import com.zhw.enums.ResultEnum;
import com.zhw.service.SellerService;
import com.zhw.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 卖家用户
 * Created by 廖师兄
 * 2017-07-30 15:28
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private StringRedisTemplate redisTemplate;  //只需要向redis写String类型的值

    @Autowired
    private ProjectUrlConfig projectUrlConfig;
    /*
    *【扫码登录地址】http://wxtest.nat300.top/sell/wechat/qrAuthorize?returnUrl= http://wxtest.nat300.top/sell/seller/login.成功后会跳转至returnUrl+openid
    */
    @GetMapping("/login")    //get请求，可以在url里面设置openid
    public ModelAndView login(@RequestParam("openid") String openid,
                              HttpServletResponse response,
                              Map<String, Object> map) {

        //1. openid去和数据库里的数据匹配
        SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid);
        if (sellerInfo == null) {   //未查到用户信息
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url", "/sell/seller/order/list");  //跳到顶大你列表页
            return new ModelAndView("common/error");   //调到错误界面
        }

        //2. 设置token至redis
        String token = UUID.randomUUID().toString();// 一个token对应一个openid
        Integer expire = RedisConstant.EXPIRE;  //过期时间

        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS); //拿到操作在设置

        //3. 设置token至cookie
        CookieUtil.set(response, CookieConstant.TOKEN, token, expire);   //设置到cookie  cookie名是token

        return new ModelAndView("redirect:" + projectUrlConfig.getSell() + "/sell/seller/order/list");

    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request,
                       HttpServletResponse response,
                       Map<String, Object> map) {
        //1. 从cookie里查询
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null) {
            //2. 清除redis
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));

            //3. 清除cookie
            CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
        }

        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);
    }
}
